package org.hawk.gwtppc.webtest.tests.fieldsuite;

import java.lang.reflect.Field;

import org.hawk.gwtppc.webtest.common.Asserts;
import org.hawk.gwtppc.webtest.common.Test;
import org.hawk.gwtppc.webtest.tests.mock.GenericClassMock;
import org.hawk.gwtppc.webtest.tests.mock.GenericSubClassMock;
import org.hawk.gwtppc.webtest.tests.mock.SubClassMock1;

public class SetTest implements Test {

	public void run() throws Throwable {
		try {
			SubClassMock1.class.getDeclaredField("serialVersionUID").setLong(null, 3L);
			Asserts.fail("Unable to set private field");
		} catch (IllegalAccessException e) {
		}
		try {
			Field fld = SubClassMock1.class.getDeclaredField("serialVersionUID");
			fld.setAccessible(true);
			fld.setLong(null, 3L);
			Asserts.fail("Unable to set final field");
		} catch (IllegalAccessException e) {
		}
		SubClassMock1 mock = new SubClassMock1();
		try {
			SubClassMock1.class.getDeclaredField("flag").setLong(mock, 5L);
			Asserts.fail("Unable to set boolean field to long");
		} catch (IllegalArgumentException e) {
		}
		
		Asserts.assertFalse(mock.flag);
		SubClassMock1.class.getDeclaredField("flag").setBoolean(mock, true);
		Asserts.assertTrue(mock.flag);
		
		GenericClassMock<?, ?, ?> mock2 = new GenericSubClassMock<String>();
		Field fld3 = GenericClassMock.class.getDeclaredField("genericField");
		fld3.setAccessible(true);
		Asserts.assertEquals(fld3.get(mock2), GenericSubClassMock.THE_STRING);
		fld3.set(mock2, "uuuu");
		Asserts.assertEquals(fld3.get(mock2), "uuuu");
	}

	public String getName() {
		return "set(Object)";
	}

}
